package com.thesis.client;

import com.thesis.conectify.Connectify;
import com.thesis.utils.Defines;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class UserEditActivity extends Activity {
	EditText tv_firstname;
	EditText tv_lastname;
	EditText tv_dayofbirth;
	EditText tv_address;
	EditText tv_phone;
	String[] edit;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_user_edit);
		
		edit = getIntent().getStringArrayExtra(Defines.USER_DETAIL);
		tv_firstname = (EditText) findViewById(R.id.ue_et_firstname);
		tv_lastname = (EditText) findViewById(R.id.ue_et_lastname);
		tv_dayofbirth = (EditText) findViewById(R.id.ue_et_dayofbirth);
		tv_address = (EditText) findViewById(R.id.ue_et_address);
		tv_phone = (EditText) findViewById(R.id.ue_et_phone);
		
		tv_firstname.setText(edit[1]);
		tv_firstname.setTypeface(Typeface.DEFAULT_BOLD);
		
		tv_lastname.setText(edit[0]);
		tv_lastname.setTypeface(Typeface.DEFAULT_BOLD);
		
		tv_dayofbirth.setText(edit[2]);
		tv_dayofbirth.setTypeface(Typeface.DEFAULT_BOLD);
		
		tv_address.setText(edit[3]);
		tv_address.setTypeface(Typeface.DEFAULT_BOLD);
		
		tv_phone.setText(edit[4]);
		tv_phone.setTypeface(Typeface.DEFAULT_BOLD);
		
		Button btn_cancel = (Button) findViewById(R.id.ue_b_cance);
		Button btn_ok = (Button) findViewById(R.id.ue_b_ok);
		btn_ok.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				//startActivity(new Intent(UserEditActivity.this,UserActivity.class));
				new EditUserDetail().execute();
			}
		});
		btn_cancel.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				finish();
				
			}
		});
		
	}

	public class EditUserDetail extends AsyncTask<String, String, String>{

		@Override
		protected String doInBackground(String... params) {
			String firstname = tv_firstname.getText().toString();
			String lastname = tv_lastname.getText().toString();
			String dayofbirth = tv_dayofbirth.getText().toString();
			String address = tv_address.getText().toString();
			String phone = tv_phone.getText().toString();
			Connectify connect = new Connectify(UserEditActivity.this);
			connect.editUser(Integer.parseInt(edit[5]), 
					firstname, lastname, dayofbirth, address, phone);
			
			return null;
		}
		@Override
		protected void onPostExecute(String result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			Intent i = getIntent();
			
			setResult(100, i);
			finish();
		}
		
	}

}
